package legumes.client.ui.otherboxes;

import legumes.client.evt.main.UserConnectedEvent;
import legumes.client.evt.main.UserConnectedEventHandler;
import legumes.client.evt.manager.EventBusManager;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEvent;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEventHandler;
import legumes.client.evt.store.StoreResearchByZipCodeChangedEvent;
import legumes.client.resources.PNResources;
import legumes.client.ui.widgets.form.item.ZipCodeFormItem;

import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyUpEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyUpHandler;

public class StoreReseachByZipCodeBoxPanel extends Window {
	
	private ZipCodeFormItem zipCodeFormItem;
	
	private ButtonItem validateItem;
	private DynamicForm df;
	
	public StoreReseachByZipCodeBoxPanel(){
		super();
		setWidth100();
		setHeight("80");
		setMargin(5);
		setMembersMargin(10);
		setTitle("<img src=\"" + PNResources.INSTANCE.mapIcon().getURL() + "\" />&nbsp;Rechercher un magasin");
		setShowCloseButton(false);
        setShowMinimizeButton(true);
		setCanDrag(false);
		setCanDragReposition(false);
		
		buildPanel();
		addItem(df);
		
		zipCodeFormItem.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				String value = zipCodeFormItem.getValueAsString();
				if(value!=null && (value.length()==2 || value.length()==5)){
					onBtnValidateClicked();
				}
			}
		});
		bind();
	}
	
	private void buildPanel() {
		df = new DynamicForm();
		df.setNumCols(4);
		zipCodeFormItem = new ZipCodeFormItem();
		zipCodeFormItem.setWidth(50);
		zipCodeFormItem.setHint("");
		
		validateItem = new ButtonItem("validateItem", "");
		validateItem.setStartRow(false);
		validateItem.setIcon(PNResources.INSTANCE.validateIcon().getURL());
		validateItem.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				onBtnValidateClicked();
			}
		});
		df.setItems(zipCodeFormItem, validateItem);
	}
	
	

	protected void onBtnValidateClicked() {
		if(df.validate()){
			EventBusManager.getInstance().getEventBus().fireEvent(new StoreResearchByZipCodeChangedEvent(zipCodeFormItem.getValueAsString()
					));
		}
	}

	private void bind(){
		EventBusManager.getInstance().getEventBus().addHandler(
				PlaceDeliverySelectionChangedEvent.TYPE, 
				new PlaceDeliverySelectionChangedEventHandler(){

					@Override
					public void onStoreSelectionChanged(
							PlaceDeliverySelectionChangedEvent event) {
						if( isVisible() ){
							setVisible(false);
						}
					}
				});
		
		EventBusManager.getInstance().getEventBus().addHandler(
				UserConnectedEvent.TYPE, 
				new UserConnectedEventHandler(){

					@Override
					public void onUserConnected(UserConnectedEvent event) {
						if( isVisible() ){
							setVisible(false);
						}
					}
				});
	}
	
}